package com.example.myapplication;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

//重写生命周期内的五大方法：onCreate onStart onPause onStop onResume

public class MainActivity extends AppCompatActivity {

    private TextView myText=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.v("myLog","onCreate");
        myText=findViewById(R.id.firstText);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.v("myLog","onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.v("myLog","onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.v("myLog","onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.v("myLog","onStop");
    }

    public void firstClick(View view) {
        //显示意图的使用,实现页面的跳转
        //第一个参数是当前页，第二个参数是跳转过去的页面
        Intent intent=new Intent(MainActivity.this,SecondActivity.class);
        //用意图传递数据
        intent.putExtra("UserName","Jack");
        intent.putExtra("Age",23);

//        //用bundle传数据
//        Bundle bundle=new Bundle();
//        bundle.putString("UserName","Jack");
//        bundle.putInt("Age",23);
//        intent.putExtras(bundle);
        //全部设置完成后再启动意图
//        this.startActivity(intent);   

        //数据回传，此步骤不需要启动意图
        startActivityForResult(intent,666);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==666&&resultCode==888)
        {
            myText.setText(data.getStringExtra("data"));
            Toast.makeText(MainActivity.this,data.getStringExtra("data"),Toast.LENGTH_SHORT).show();
        }
    }
}